﻿/// <reference path="prototype.js" />
/// <reference path="Snake.class.js" />

var Player = Class.create({
    initialize: function(nickname, score, snake) { 
        this.nickname = nickname;
        this.score = score;
        this.snake = snake;
                
    },
    
    setSnake: function(snake) {
        this.snake = snake;
    },
    
    getSnake: function() {
        return this.snake;
    },
    
    serialize: function() {
        return escape(Object.toJSON(this));
    },
    
    remove: function() {
      this.snake.remove();
    }
    
    /*
    toJSON: function() {
        
    }*/

});

//static method
Player.unserialize = function(data, gameboard) {
   try {
			data = unescape(data).evalJSON();
		} catch(err) {
			if(console) console.error("Invalid JSON data in Player.class.js : " + data);
			return undefined;
		}
    var p = new Player(data.nickname, data.score);
    var s = Snake.unserialize(data.snake, gameboard);
    p.setSnake(s);
    return p;
}